/**
 * 
 */
package br.com.skynix.tcmc.base.model;

import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

/**
 * @author mgrb
 * 
 */
@MappedSuperclass
public abstract class EntidadeAuditavel {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Column(name = "data_criacao", updatable = false,
			columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
	private Timestamp dataCriacao;

	@NotNull
	@Column(name = "data_ult_atualizacao", nullable = false)
	private Timestamp dataUltimaAtualizacao;

	@NotNull
	@Column(nullable = false, length = 150)
	private String responsavel;

	@NotNull
	@Column(nullable = false, length = 20)
	private String ip;

	@Column(name = "flag_deletado", columnDefinition = "BOOLEAN DEFAULT FALSE")
	private Boolean deletado;

	@Column(name = "flag_ativo", columnDefinition = "BOOLEAN DEFAULT TRUE")
	private Boolean ativo;

	@Version
	private Integer versao;

	/**
	 * 
	 */
	public EntidadeAuditavel() {
		ativo = true;
		deletado = false;
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the dataCriacao
	 */
	public Timestamp getDataCriacao() {
		return dataCriacao;
	}

	/**
	 * @param dataCriacao the dataCriacao to set
	 */
	public void setDataCriacao(Timestamp dataCriacao) {
		this.dataCriacao = dataCriacao;
	}

	/**
	 * @return the dataUltimaAtualizacao
	 */
	public Timestamp getDataUltimaAtualizacao() {
		return dataUltimaAtualizacao;
	}

	/**
	 * @param dataUltimaAtualizacao the dataUltimaAtualizacao to set
	 */
	public void setDataUltimaAtualizacao(Timestamp dataUltimaAtualizacao) {
		this.dataUltimaAtualizacao = dataUltimaAtualizacao;
	}

	/**
	 * @return the responsavel
	 */
	public String getResponsavel() {
		return responsavel;
	}

	/**
	 * @param responsavel the responsavel to set
	 */
	public void setResponsavel(String responsavel) {
		this.responsavel = responsavel;
	}

	/**
	 * @return the ip
	 */
	public String getIp() {
		return ip;
	}

	/**
	 * @param ip the ip to set
	 */
	public void setIp(String ip) {
		this.ip = ip;
	}

	/**
	 * @return the deleted
	 */
	public Boolean getDeletado() {
		return deletado;
	}

	/**
	 * @param deleted the deleted to set
	 */
	public void setDeletado(Boolean deleted) {
		this.deletado = deleted;
	}

	/**
	 * @return the active
	 */
	public Boolean getAtivo() {
		return ativo;
	}

	/**
	 * @param active the active to set
	 */
	public void setAtivo(Boolean active) {
		this.ativo = active;
	}

	/**
	 * @return the version
	 */
	public Integer getVersao() {
		return versao;
	}

	/**
	 * @param version the version to set
	 */
	public void setVersao(Integer version) {
		this.versao = version;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}
		EntidadeAuditavel entidade = (EntidadeAuditavel) obj;
		return new EqualsBuilder().append(id, entidade.getId()).isEquals();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		// you pick a hard-coded, randomly chosen, non-zero, odd number
		// ideally different for each class
		return new HashCodeBuilder(17, 37).append(id).toHashCode();
	}
}
